package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.AcademicConferencesMapper;
import com.zhijian.medical.entity.po.AcademicConferences;
import com.zhijian.medical.entity.vo.request.AcademicConferencesSearchVo;
import com.zhijian.medical.entity.vo.request.AcademicConferencesSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.AcademicConferencesVo;
import com.zhijian.medical.service.AcademicConferencesService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 举办或参加学术会议业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-05-09 19:38:04
 */
@Slf4j
@Service
public class AcademicConferencesServiceImpl implements AcademicConferencesService {

	@Autowired
    private AcademicConferencesMapper mapper;

    @Override
    public AcademicConferences selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AcademicConferencesVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(AcademicConferences academicConferences) {
        Date now = new Date();
        academicConferences.setCreateTime(now);
        academicConferences.setUpdateTime(now);
        return mapper.insert(academicConferences) > 0 ? academicConferences.getId() : 0L;
    }

    @Override
    public boolean update(AcademicConferences academicConferences) {
        Date now = new Date();
        academicConferences.setUpdateTime(now);
        return mapper.updateByPrimaryKey(academicConferences) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AcademicConferences record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(AcademicConferencesSaveOrUpdateVo saveOrUpdateVo) {
        AcademicConferences academicConferences = new AcademicConferences();
        BeanUtils.copyProperties(saveOrUpdateVo, academicConferences);
        return this.insert(academicConferences) != 0;
    }

    @Override
    public boolean modify(AcademicConferencesSaveOrUpdateVo saveOrUpdateVo) {
        AcademicConferences old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        AcademicConferences academicConferences = new AcademicConferences();
        BeanUtils.copyProperties(saveOrUpdateVo, academicConferences);
        return this.update(academicConferences);
    }

    @Override
    public AcademicConferencesVo selectOne(AcademicConferencesSearchVo academicConferencesSearchVo) {
        return mapper.selectOne(academicConferencesSearchVo);
     }

    @Override
    public Integer count(AcademicConferencesSearchVo academicConferencesSearchVo) {
        return mapper.count(academicConferencesSearchVo);
    }

    @Override
    public List<AcademicConferencesVo> selectList(AcademicConferencesSearchVo academicConferencesSearchVo) {
        return mapper.selectList(academicConferencesSearchVo);
    }

    @Override
    public Page<AcademicConferencesVo> selectPage(AcademicConferencesSearchVo academicConferencesSearchVo) {
        AssertUtil.check(Objects.isNull(academicConferencesSearchVo.getPageNum()) || Objects.isNull(academicConferencesSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(academicConferencesSearchVo);
        //分页
        Page<AcademicConferencesVo> page = new Page<>(true, count, academicConferencesSearchVo.getPageNum(), academicConferencesSearchVo.getPageSize());
        academicConferencesSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AcademicConferencesVo> responseVos = this.selectList(academicConferencesSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AcademicConferences> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(AcademicConferencesSearchVo academicConferencesSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(academicConferencesSearchVo);
    }
    
}